-- Tags: long
-- Tag long: in parallel runs could exceed 60 seconds
{# vim: ft=sql #}

SET max_memory_usage=0;
DROP DICTIONARY IF EXISTS dict;
DROP TABLE IF EXISTS dict_data;

CREATE TABLE dict_data (key UInt64, value UInt64) Engine=Memory();
INSERT INTO dict_data SELECT number, number%10 FROM numbers(3_000_000);

SET max_memory_usage='4Mi';
{% for layout in [
    'FLAT(INITIAL_ARRAY_SIZE 3_000_000 MAX_ARRAY_SIZE 3_000_000)',
    'HASHED()',
    'HASHED_ARRAY()',
    'HASHED_ARRAY(SHARDS 2)',
    'SPARSE_HASHED()',
    'SPARSE_HASHED(SHARDS 2 /* shards are special, they use threads */)',
] %}
CREATE DICTIONARY dict (key UInt64, value UInt64) PRIMARY KEY key SOURCE(CLICKHOUSE(TABLE dict_data)) LIFETIME(0) LAYOUT({{layout}});
SYSTEM RELOAD DICTIONARY dict;
-- assert that dictionary in memory takes more than 20MB, that way for each
-- shard we will have 10MB, that way we ensure that the allocations will be
-- definitely correct for the memory tracker to hit the MEMORY_LIMIT_EXCEEDED
-- error.
SELECT throwIf(bytes_allocated < 20e6, 'Memory constraints does not matched for layout {{layout}}') FROM system.dictionaries WHERE database = currentDatabase() AND name = 'dict' FORMAT Null;
DROP DICTIONARY dict;

CREATE DICTIONARY dict (key UInt64, value UInt64) PRIMARY KEY key SOURCE(CLICKHOUSE(TABLE dict_data)) LIFETIME(0) LAYOUT({{layout}});
SELECT dictGet('dict', 'value', 1::UInt64) FORMAT Null;
DROP DICTIONARY dict;
{% endfor %}
